#pragma once

#include <TCHAR.h>

#pragma warning (disable: 4996)

wchar_t letters[] = { 'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z' };
int  degrees[] = { -90, -84, -72, -64, -56, -48, -40, -32, -24, -16, -8,   0,   8,  16,   24,  32,  40,  48,  56,  64,  72,  84 };

wchar_t negLetters[] = { 'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M' };
int  negDegrees[] = { -90, -84, -72, -64, -56, -48, -40, -32, -24, -16, -8 };

wchar_t posLetters[] = { 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z' };
int  posDegrees[] = {  0,   8,  16,  24,  32,  40,  48,  56,  64,  72,  84 };
int arrayLength = 22;


wchar_t getLatZone(double latitude)
{
	int latIndex = -2;
	int lat = (int)latitude;

	int posLen = sizeof(posLetters) / sizeof(wchar_t);
	int negLen = sizeof(negLetters) / sizeof(wchar_t);

	if (lat >= 0)
	{
		for (int i = 0; i < posLen; i++)
		{
			if (lat == posDegrees[i])
			{
				latIndex = i;
				break;
			}

			if (lat > posDegrees[i])
			{
				continue;
			}
			else
			{
				latIndex = i - 1;
				break;
			}
		}
	}
	else
	{
		for (int i = 0; i < negLen; i++)
		{
			if (lat == negDegrees[i])
			{
				latIndex = i;
				break;
			}

			if (lat < negDegrees[i])
			{
				latIndex = i - 1;
				break;
			}
			else
			{
				continue;
			}
		}
	}

	if (latIndex == -1)
	{
		latIndex = 0;
	}

	if (lat >= 0)
	{
		if (latIndex == -2)
		{
			latIndex = posLen - 1;
		}
		return posLetters[latIndex];
	}
	else
	{
		if (latIndex == -2)
		{
			latIndex = negLen - 1;
		}
		return negLetters[latIndex];
	}
}

void getLongZone(double longitude, wchar_t* szLongZone)
{
	double longZone = 0;
	if (longitude < 0.0)
	{
		longZone = ((180.0 + longitude) / 6) + 1;
	}
	else
	{
		longZone = (longitude / 6) + 31;
	}

	_itot((int)longZone, szLongZone, 10);
	if (wcslen(szLongZone) == 1)
	{
		wchar_t szNewVal[MAX_LENGTH] = {'0'};
		_tcscat(szNewVal, szLongZone);
		_tcscpy(szLongZone, szNewVal);
	}
}


//int getLatZoneDegree(String letter)
//{
//    wchar_t ltr = letter.charAt(0);
//    for (int i = 0; i < arrayLength; i++)
//    {
//        if (letters[i] == ltr)
//        {
//            return degrees[i];
//        }
//    }
//    return -100;
//}

